<?php
class MResponse {
    private $_module;
    private $_vars = array();
    private $_content;
    private $_script;
    private $_stylesheet;

    public function  __construct($vars = array(), $content = NULL) {
        $this->_vars = $vars;
        $this->_content = $content;
    }

    /**
     * @return Array : Variables defined in the response
     */
    public function getVars() {
        return $this->_vars;
    }

    public function setVars($_vars) {
        $this->_vars = $_vars;
    }

    public function assignVar($name, $value) {
        $this->_vars[$name] = $value;
    }

    /**
     * @return String : XHTML Code of the response
     */
    public function getContent() {
        return $this->_content;
    }

    public function setContent($_content) {
        $this->_content = $_content;
    }

    /**
     * @return String : XHTML Code for scripts defined in the response
     */
    public function getScript() {
        return $this->_script;
    }

    public function setScript($_script) {
        $this->_script = $_script;
    }

    /**
     * @return String : XHTML Code for stylesheets defined in the response
     */
    public function getStylesheet() {
        return $this->_stylesheet;
    }

    public function setStylesheet($_stylesheet) {
        $this->_stylesheet = $_stylesheet;
    }

    /**
     * @return String : Name of the module
     */
    public function getModule() {
        return $this->_module;
    }

    public function setModule($_module) {
        $this->_module = $_module;
    }

    /**
     * Render the response with the template instance
     */
    public function render(){
        ob_start();
        extract($this->_vars);
        $tmpStylesheets = MTemplate::getInstance()->getStylesheets();
        if (!empty($tmpStylesheets)){
            $mlStylesheets = MTemplate::getInstance()->renderStylesheets();
            $mlStylesheets .= $this->_stylesheet;
        } else {
            $mlStylesheets = $this->_stylesheet;
        }
        $tmpScripts = MTemplate::getInstance()->getScripts();
        if (!empty($tmpScripts)){
            $mlScripts = MTemplate::getInstance()->renderScripts();
            $mlScripts .= $this->_script;
        } else {
            $mlScripts = $this->_script;
        }
        $mlContent = $this->_content;
        MTemplate::getInstance()->setModule($this->_module);
        extract(MTemplate::getInstance()->getVars());
        $templatePath = MTemplate::getInstance()->getLayoutPath();
        if (file_exists($templatePath))
            require ($templatePath);
        else
            require (MD_LIB.DS.'views'.DS.'baseTemplate.php');
        $content = ob_get_contents();
        ob_end_clean();
        echo $content;
        MTemplate::getInstance()->clearVars();
    }

    /**
     * Render the response (only the content)
     */
    public function renderPartial($return = false){
        if ($return){
            return $this->_content;
        } else {
            echo $this->_content;
        }
    }
    
    public function __destruct() {
        unset($this->_vars);
    }
    
}
?>
